<script>
import axios from "axios";
import {ref} from "vue";

export default {
  setup() {
    //修改 弹窗
    const updateVisible = ref(false);
    //新增弹窗
    const saveVisible = ref(false);
    return {
      updateVisible,
      saveVisible,
    };
  },
  name: "Manage",
  data() {
    return {
      room:{
        roomId:'',
        roomName:'',
        description:''
      },
      pageDto: {
        pageNum: 1,
        pageSize: 10,
      },
      total:0,
      pages:0,
      roomList:[]
    }
  },
  methods:{
    submitForm(){
      axios.post("/oa/room/getAllRoom",this.pageDto).then((res)=>{
        this.pageDto.pageNum=res.data.data.pageNum;
        this.pageDto.pageSize = res.data.data.pageSize;
        this.total=res.data.data.total;
        this.pages=res.data.data.pages;
        this.roomList=res.data.data.list;
      })
    },
    deleteRoom(roomId){
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.delete(`/oa/room/deleteRoom/${roomId}`).then((res)=>{
          if(res.data.code === 1){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.submitForm();
          }else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            });
          }
        })
      }).catch(
          () => {
            this.$message({
              type: 'info',
              message: '已取消'
            });
          }
      );
    },

    getRoom(roomId){
      this.updateVisible = true;
    // 根据id 查询数据
      axios.get(`/oa/room/getRoomById/${roomId}`).then((res)=>{
        this.room = res.data.data;
      })
    },
    updateRoom(){
      axios.put("/oa/room/updateRoom",this.room).then((res)=>{
        if(res.data.code === 1){
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.updateVisible = false;
          this.submitForm();
        }else {
          this.$message({
            type: 'error',
            message: '修改失败!'
          });
        }
      })
    },
    saveRoom(){
      if(this.room.roomName === null || this.room.roomName === undefined || this.room.roomName === ""){
        alert("不能为空")
        return;
      }
      if(this.room.description === null || this.room.description === undefined || this.room.description === ""){
        alert("标题不能为空")
        return;
      }
      axios.post("/oa/room/saveRoom",this.room).then((res)=>{
        if(res.data.code === 1){
          this.room = {};
          this.$message({
            type: 'success',
            message: '新增成功!'
          });
          this.saveVisible = false;
          this.submitForm();
        }else {
          this.$message({
            type: 'error',
            message: '修改成功!'
          });
        }
      })
    },
    insertRoom(){
      this.saveVisible = true
      this.room = {}
    }
  },
  created() {
    this.submitForm();
  }
}
</script>

<template>
  <div class="titleBox">
    会议室管理
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <el-button  size="small" type="primary" @click="insertRoom()">
    新增
  </el-button>
  </div>


  <!-- 首页表格信息显示 -->
  <el-table :data="this.roomList" style="width: 100%">

    <el-table-column label="会议室序号">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.roomId}}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="会议室名称">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.roomName}}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="会议室描述">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.description }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="操作">
      <template #default="scope">
          <el-button size="small" @click="getRoom(scope.row.roomId)">
            修改
          </el-button>
        <el-button size="small" type="danger" @click="deleteRoom(scope.row.roomId)">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>


<!--// 修改弹窗-->
  <el-dialog
      v-model="updateVisible"
      title="修改 会议室"
      width="500"
      :before-close="handleClose"
  >
    <el-form
        style="max-width: 500px;margin: 0 auto"
        :model="car"
        label-width="auto"
        class="demo-ruleForm"
    >
      <el-form-item label="请输入会议室名称" prop="name">
        <el-input
            v-model="room.roomName"
            type="text"
            autocomplete="off"
            aria-required="true"
        />
      </el-form-item>
      <el-form-item label="请输入会议室描述" prop="name">
        <el-input
            v-model="room.description"
            type="text"
            autocomplete="off"
            aria-required="true"
        />
      </el-form-item>
    </el-form>


    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="updateRoom()">
          保存
        </el-button>
        <el-button @click="saveVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>

<!--  // 新增弹窗-->
  <el-dialog
      v-model="saveVisible"
      title="新增会议室"
      width="500"
      :before-close="handleClose"
  >
    <el-form
        style="max-width: 500px;margin: 0 auto"
        :model="car"
        label-width="auto"
        class="demo-ruleForm"
    >
      <el-form-item label="会议室名称：" prop="name">
        <el-input
            v-model="room.roomName"
            type="text"
            autocomplete="off"
            aria-required="true" placeholder="请输入会议室名称"
        />
      </el-form-item>
      <el-form-item label="会议室描述：" prop="name">
        <el-input
            v-model="room.description"
            type="text"
            autocomplete="off"
            aria-required="true"
            placeholder="请输入会议室描述"
        />
      </el-form-item>
    </el-form>


    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="saveRoom()">
          保存
        </el-button>
        <el-button @click="saveVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="less">

</style>
